/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  PopHaoYouMoreView.java   
 * @Package com.swift.widget   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-7-6 下午5:04:41   
 * @version V1.0     
 */
package com.swift.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.swift.R;
import com.swift.utils.DeviceUtil;

public class HaoYouMorePopView {

	/**
	 * @ClassName: PopHaoYouMoreView
	 * @Description:TODO(这里用一句话描述这个类的作用)
	 * @author: HYM
	 * @date: 2017-7-6 下午5:04:41
	 * 
	 */
	private HaoYouMorePopInterface mHaoYouMorePopInterface;
	private Context context;
	private PopupWindow mPopupWindow;

	public HaoYouMorePopView(Context context,
			HaoYouMorePopInterface haoYouMorePopInterface) {
		this.context = context;
		this.mHaoYouMorePopInterface = haoYouMorePopInterface;
		init();
	}

	/**
	 * @author: HYM
	 * @Title: init
	 * @Description: TODO(初始化一个新的PopWindow)
	 * @param:
	 * @return: void
	 * @return: 2017-5-5
	 * @throws
	 */
	private void init() {
		// 得到内容布局
		View view = LayoutInflater.from(context).inflate(R.layout.pop_haoyou_more,
				null);

		// 备注按钮
		TextView tvInfo = (TextView) view.findViewById(R.id.tvInfo);
		tvInfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mHaoYouMorePopInterface.OnInfoListener();
			}
		});

		// 删除按钮
		TextView tvDelete = (TextView) view.findViewById(R.id.tvDelete);
		tvDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mHaoYouMorePopInterface.OnDeleteListener();
			}
		});

		mPopupWindow = new PopupWindow(view,
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		mPopupWindow.setFocusable(true); // 让popWindow获取焦点
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); // 设置popWindow背景颜色
//		mPopupWindow.setAnimationStyle(R.style.AnimSlide);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				hidePopWindow();
			}
		});
	}

	/**
	 * @author: HYM
	 * @Title: showPopWindow
	 * @Description: TODO(显示弹出菜单)
	 * @param:
	 * @return: void
	 * @return: 2017-4-26
	 * @throws
	 */
	public void showPopWindow(View root) {
		mPopupWindow.showAsDropDown(root);
		backgroundAlpha(0.5F);
		mHaoYouMorePopInterface.OnShowListener();
	}

	/**
	 * @author: HYM
	 * @Title: hidePopWindow
	 * @Description: TODO(关闭弹出菜单)
	 * @param:
	 * @return: void
	 * @return: 2017-4-26
	 * @throws
	 */
	public void hidePopWindow() {
		if (mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
		backgroundAlpha(1F);
		mHaoYouMorePopInterface.OnHideListener();
	}

	/**
	 * 设置添加屏幕的背景透明度
	 * 
	 * @param bgAlpha
	 */
	public void backgroundAlpha(float bgAlpha) {
		Activity activity = (Activity) context;
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.alpha = bgAlpha; // 0.0-1.0
		activity.getWindow().setAttributes(lp);
		activity.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	}
}
